<?php

/**
 * 品酒会管理
 * $Author: sam iokdo@139.com
 * 2011-1-15 16:00
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);

$exc = new exchange($ecs->table("vips"), $db, 'id', 'username');

$link = array();
/*------------------------------------------------------ */
//-- vip信息列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    assign_query_info();
    $smarty->assign('ur_here',     $_LANG['vip_list']);
    $smarty->assign('full_page',   1);

    $vip_list = vip_list();

    $smarty->assign('vip_list',    $vip_list['vip_list']);
    $smarty->assign('filter',       $vip_list['filter']);
    $smarty->assign('record_count', $vip_list['record_count']);
    $smarty->assign('page_count',   $vip_list['page_count']);

    $smarty->display('vip_list.htm');
}
/*------------------------------------------------------ */
//-- ajax列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $vip_list = vip_list();
    $smarty->assign('vip_list',    $vip_list['vip_list']);
    $smarty->assign('filter',       $vip_list['filter']);
    $smarty->assign('record_count', $vip_list['record_count']);
    $smarty->assign('page_count',   $vip_list['page_count']);

    $sort_flag  = sort_flag($vip_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['id']);

    make_json_result($smarty->fetch('vip_list.htm'), '', array('filter' => $vip_list['filter'], 'page_count' => $vip_list['page_count']));
}
/*------------------------------------------------------ */
//-- 删除贺卡
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    /* 检查权限 */
    check_authz_json('vip_manage');

    $id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);

    if ($exc->drop($id))
    {
        
        $url = 'vip.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
        ecs_header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- 添加新包装
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('vip_manage');

    $smarty->assign('vip',     $vip);
    $smarty->assign('ur_here',     $_LANG['vip_add']);
    $smarty->assign('action_link', array('text' => $_LANG['vip_list'], 'href' => 'vip.php?act=list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('vip_info.htm');

}
elseif ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('vip_manage');

     /*处理图片*/
    $img_name = basename($image->upload_image($_FILES['img'],"vipimg"));

    /*插入数据*/
    $sql = "INSERT INTO ".$ecs->table('vip')."(title, link, content, img)
            VALUES ('$_POST[title]', '$_POST[link]', '$_POST[content]', '$img_name')";
    $db->query($sql);

    admin_log($_POST['title'],'add','vip');

    /*添加链接*/
    $link[0]['text'] = $_LANG['continue_add'];
    $link[0]['href'] = 'vip.php?act=add';

    $link[1]['text'] = $_LANG['back_list'];
    $link[1]['href'] = 'vip.php?act=list';

    sys_msg($_POST['title'].$_LANG['vipadd_succeed'],0, $link);
}

/*------------------------------------------------------ */
//-- 编辑包装
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('vip_manage');

    $sql = "SELECT id, username, phone, content FROM " .$ecs->table('vips'). " WHERE id='$_REQUEST[id]'";
    $vip = $db->GetRow($sql);

    $smarty->assign('ur_here',     $_LANG['vip_edit']);
    $smarty->assign('action_link', array('text' => $_LANG['vip_list'], 'href' => 'vip.php?act=list&' . list_link_postfix()));
    $smarty->assign('vip',        $vip);
    $smarty->assign('form_action', 'update');

    assign_query_info();
    $smarty->display('vip_info.htm');
}
elseif ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('vip_manage');

    $param = "username = '$_POST[username]', phone = '$_POST[phone]', content = '$_POST[content]'";

    if ($exc->edit($param,  $_POST['id']))
    {

        $link[0]['text'] = '返回vip信息列表';
        $link[0]['href'] = 'vip.php?act=list&' . list_link_postfix();

        $note = sprintf("更新[%s]的信息成功！", $_POST['username']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}

elseif ($_REQUEST['act'] == 'edit_vip_name')
{
    check_authz_json('vip_manage');
    $id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $vip_name = empty($_REQUEST['val']) ? '' : json_str_iconv(trim($_REQUEST['val']));

    if (!$exc->is_only('vip_name', $vip_name, $id))
    {
        make_json_error(sprintf($_LANG['vipname_exist'], $vip_name));
    }
    $old_vip_name = $exc->get_name($id);
    if ($exc->edit("vip_name='$vip_name'", $id))
    {
        admin_log(addslashes($old_vip_name), 'edit', 'vip');
        make_json_result(stripcslashes($vip_name));
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑卡片费用
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_vip_fee')
{
    check_authz_json('vip_manage');
    $id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $vip_fee = empty($_REQUEST['val']) ? 0.00 : floatval($_REQUEST['val']);

    $vip_name = $exc->get_name($id);
    if ($exc->edit("vip_fee ='$vip_fee'", $id))
    {
        admin_log(addslashes($vip_name), 'edit', 'vip');
        make_json_result($vip_fee);
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑免费额度
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_free_money')
{
    check_authz_json('vip_manage');
    $id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $free_money = empty($_REQUEST['val']) ? 0.00 : floatval($_REQUEST['val']);

    $vip_name = $exc->get_name($id);
    if ($exc->edit("free_money ='$free_money'", $id))
    {
        admin_log(addslashes($vip_name), 'edit', 'vip');
        make_json_result($free_money);
    }
    else
    {
        make_json_error($db->error());
    }
}

function vip_list()
{
    $result = get_filter();
    if ($result === false)
    {
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        /* 分页大小 */
        $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('vips');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * ".
               " FROM ".$GLOBALS['ecs']->table('vips').
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $vip_list = $GLOBALS['db']->getAll($sql);
    foreach($vip_list as $key => $info){
        $vip_list[$key]['create_time'] = date("Y-m-d H:i:s",$info['create_time']);
    }

    $arr = array('vip_list' => $vip_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>